How to Install ZRAM to Boost Ubuntu Performance

If you’re using Ubuntu and want to improve your system’s performance, especially on machines with limited RAM, ZRAM is a great solution, as it creates a compressed block device in RAM, which helps to manage memory more efficiently.

TecMint Weekly Newsletter
Get the Learn Linux 7 Days Crash Course free when you join 34,000+ Linux professionals reading every Thursday.
Check your email for a magic link to get started.
Something went wrong. Please try again.

What is ZRAM?

ZRAM is a Linux kernel module that allows you to create a compressed block device in RAM, that effectively increases the amount of usable memory on your system by compressing data stored in RAM, which can be especially useful for systems with limited physical memory, allowing them to run more applications simultaneously without slowing down.

This guide will walk you through installing and configuring ZRAM on Ubuntu.

Step 1: Check Ubuntu RAM Usage

Before installing ZRAM, it’s good to know how much RAM your system is currently using by running the free command.

free -h
Check Ubuntu RAM Usage
Check Ubuntu RAM Usage

This command shows your total, used, and free memory. Keep this information in mind to see how ZRAM improves your system performance later.

Step 2: Install ZRAM in Ubuntu

ZRAM is included in the Linux kernel, so you only need to load the ZRAM module and verify it.

sudo modprobe zram
lsmod | grep zram
Load ZRAM in Ubuntu
Load ZRAM in Ubuntu

If you see zram in the output, it means the module is successfully loaded.

Step 3: Configure ZRAM in Ubuntu

Next, you’ll need to configure ZRAM to use it effectively, which involves creating a script that will set up ZRAM with your desired configuration.

sudo nano /etc/systemd/system/zram.service

Copy and paste the following configuration into the file.

[Unit]
Description=ZRAM Configuration
After=local-fs.target

[Service]
Type=oneshot
ExecStart=/usr/sbin/modprobe zram
ExecStart=/bin/sh -c 'echo 2G > /sys/block/zram0/disksize'
ExecStart=/bin/sh -c 'mkswap /dev/zram0'
ExecStart=/bin/sh -c 'swapon /dev/zram0'
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

Note: Change 2G in the ExecStart line that sets disksize to the size of the ZRAM you want. You can set this to any size depending on your system’s RAM. A common practice is to set it to 50-100% of your RAM size.

Now start and enable the ZRAM service with the following commands.

sudo systemctl daemon-reload
sudo systemctl enable zram.service
sudo systemctl start zram.service

After starting the service, you can check if ZRAM is active and being used.

swapon --show
Check ZRAM Status
Check ZRAM Status

You should see /dev/zram0 listed, indicating that ZRAM is active and serving as a swap space.

Step 4: Monitor ZRAM Memory Usage

To see how ZRAM is affecting your memory usage, you can use the free -h command again:

free -h

You should notice that the amount of free memory has increased, as ZRAM allows your system to use compressed memory effectively.

Step 5: Adjust ZRAM Configuration (Optional)

If you find that your system is not performing as expected, you might want to adjust the size of ZRAM.

To do this, stop the ZRAM service.

sudo systemctl stop zram.service

Open the configuration file you created earlier.

sudo nano /etc/systemd/system/zram.service

Change the disksize value to your desired size, then start the ZRAM service again.

sudo systemctl start zram.service
Conclusion

Using ZRAM on Ubuntu can significantly improve your system’s performance, especially if you have limited RAM. By following the steps in this guide, you can easily install and configure ZRAM to better manage your system’s memory.

If this article helped, with someone on your team.

TecMint Weekly Newsletter
Get the Learn Linux 7 Days Crash Course free when you join 34,000+ Linux professionals reading every Thursday.
Check your email for a magic link to get started.
Something went wrong. Please try again.
TecMint has been free for 14 years. Help keep it that way.
Google AI Overviews and tools like ChatGPT have cut into search traffic for independent tech sites like TecMint. Running this site costs over $2,000 every month for hosting, infrastructure, and paying authors to keep the content accurate and tested.

If this article helped you solve a problem, consider buying a coffee. It helps keep TecMint free, supports the authors, and keeps the project going.
☕ Buy Me a Coffee
Ravi Saive
I'm Ravi Saive, an award-winning entrepreneur and founder of several successful 5-figure online businesses, including TecMint.com, GeeksMint.com, UbuntuMint.com, and the premium learning hub Pro.Tecmint.com.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

2 Comments

Leave a Reply
  1. Hi,

    Great article indeed!

    I’m using a swap file for swap on Arch Linux. When I run swapon --show, I notice that the created /dev/zram0 is listed as a partition type.

    Is there any advantage to using zram over a swap file?

    Thanks in advance!

    Reply
    • @Jim,

      Yes, using zram can have advantages, as it compresses data before storing it in memory, making it faster than traditional swap on disk, especially on systems with limited RAM. However, a swap file may be more suitable if you need a larger swap space without using additional RAM. It depends on your use case!

      Reply

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.

Free Course
Get a free Linux course before you go.
Subscribe to TecMint Weekly and get the Learn Linux 7 Days Crash Course free. Read by 34,000+ Linux professionals every Thursday.
Something went wrong. Please try again.
Check your email for a magic link to get started.