When writing configure
scripts, it’s good to check if we are running on a 32-bit or 64-bit kernel or OS on a x86 system. There are a few ways to determine the machine architecture of a Linux system.
The first method is to use the uname
command. On a 32-bit kernel, you will get something like iX86
, or more specifically one of these: i386
, i486
, i586
or i686
.
1 2 |
$ uname -m i686 |
On a 64-bit system, you will get x86_64
.
1 2 |
$ uname -m x86_64 |
Another way is to use the getconf
utility and check the size of long int
variable. The output is self-explanatory – 32 for 32-bit and 64 for 64-bit OS.
1 2 |
$ getconf LONG_BIT 32 |
1 2 |
$ getconf LONG_BIT 64 |
The third method is to use lscpu
command and grep the Architecture
line.
1 2 |
$ lscpu | grep -i architecture Architecture: i686 |
1 2 |
$ lscpu | grep -i architecture Architecture: x86_64 |
The forth and final method is to use the arch
command.
1 2 |
$ arch i686 |
1 2 |
$ arch x86_64 |
Do not check /proc/cpuinfo
for 64-bit OS support. The /proc/cpuinfo
file will only give you the CPU capabilities, not the capabilities of the OS. A CPU may support running in 64-bit mode, but the underlying OS could be running on 32-bit. To check the CPU for 64-bit support, you can refer to this article.