Quick Links

Ubuntu wants to enable TRIM for SSDs by default in Ubuntu 14.04. In other words, Ubuntu isn't already using TRIM, so your SSD is slowing down over time. But why isn't Ubuntu already using TRIM?

This news will likely come as a surprise to many people, who assumed that Ubuntu and other Linux distributions were already using TRIM. TRIM prevents SSDs from slowing down over time and is a necessary part of SSD maintenance.

Why TRIM is Important

We've covered why TRIM is important before. When you delete a file on an old, magnetic hard drive, the computer simply marks that file as deleted. The file's data sticks around on the hard drive -- that's why deleted files can be recovered. The computer will eventually overwrite the deleted files when it overwrites their sectors with new data.

Solid-state drives (SSDs) work differently. Whenever you write a file to an SSD, the computer must first erase any data in the sectors it's writing the data to. It can't just "overwrite" the sectors in one operation -- it must first clear them, then write to the empty sectors.

This means that an SSD will slow down over time. Writing to the SSD's sectors will be quick the first time. After you delete some files and try to write to it again, it will take longer. This is a big part of the reason Google's original Nexus 7 slowed down so much over time. Google fixed this by implementing TRIM in Android 4.3. (Android also uses the Linux kernel.)

With TRIM enabled, the operating system tells the SSD each time it deletes a file. The drive can then erase the sectors containing the file's contents, so writing to the sectors will be quick in the future.

In other words, if you don't use TRIM, your SSD will slow down over time. That's why modern operating systems, including Windows 7+, Mac OS X 10.6.8+, and Android 4.3+ use TRIM. TRIM was implemented in Linux back in December 2008, but Ubuntu isn't using it by default.

Related: Do I Need to "Optimize" My SSD with Third-Party Software?

Why Doesn't Ubuntu TRIM By Default?

The real reason Ubuntu doesn't TRIM SSDs by default is because the Linux kernel's implementation of TRIM is slow and results in poor performance in normal use.

On Windows 7 and 8, Windows sends a TRIM command each time it deletes a file, telling the drive to immediately delete the bits of the file. Linux supports this when file systems are mounted with the "discard" option. However, Ubuntu -- and other distributions -- don't do this by default for performance reasons.

OpenSUSE's wiki contains some detailed information from a developer who's more familiar with the Linux kernel than we are. It's a bit dated, but is likely still true when it comes to performance:

"The kernel implementation of realtime trim in 11.2, 11.3, and 11.4 is not optimized. The spec. calls for trim supporting a vectorized list of trim ranges, but as of kernel 3.0 trim is only invoked by the kernel with a single discard / trim range and with current mid 2011 SSDs this has proven to cause a performance degradation instead of a performance increase. There are few reasons to use the kernels realtime discard support with pre-3.1 kernels. It is not known when the kernels discard functionality will be optimized to work beneficially with current generation SSDs." [Source]

In other words, the Linux kernel handles such real-time TRIM commands in a slow, unoptimized way. Enabling TRIM similar to how Windows does -- that is, using the "discard" option -- results in the system actually becoming slower than if TRIM were not used at all. Ubuntu and other Linux distributions don't enable "discard" by default for your file systems, and you shouldn't either.

ubuntu-don't-enable-discard-trim

There's Another Way

Because the Linux kernel's real-time "discard" TRIM operation doesn't perform well, most Linux distributions -- including Ubuntu -- don't use TRIM automatically. Android also didn't use TRIM until Android 4.3.

But there's another way to use TRIM. Rather than simply issuing the TRIM command each time a file is deleted, the FITRIM feature can be used. This happens via the fstrim command. Essentially, the fstrim command analyzes the file system and informs the drive which blocks are no longer needed, so the drive can discard them. This turns TRIM from a real-time operation into a scheduled task. In other words, fstrim can perform TRIM as a cron job. There's no reason not to do this. It won't slow down anything; it's just another housecleaning task the system has to perform on a schedule.

Related: Why is My Nexus 7 So Slow? 8 Ways to Speed it Up Again

In fact, this is the approach Google took with Android 4.3. Android simply runs a fstrim task occasionally to TRIM the file system, fixing the problem that slowed down all those original Nexus 7s.

Ubuntu is also looking at enabling TRIM automatically by having the system regularly run fstrim. This will hopefully be part of Ubuntu 14.04 so Ubuntu users won't be forced to deal with SSD performance degradation or run fstrim on their own.

How to Enable TRIM

We don't recommend mounting your file systems with the "discard" operation, as this will likely result in slower performance in normal use. However, you can use TRIM yourself by occasionally running the fstrim command or creating your own cronjob that runs fstrim on a schedule.

To TRIM your SSD on Ubuntu, simply open a terminal and run the following command:

sudo fstrim -v /

run-trim-on-ubuntu

You can run the above command occasionally to prevent performance degradation on SSDs. How often you need to run it depends on how often files are deleted from your SSD. You'll see an error if you try to run the command with a drive that doesn't support TRIM.

If you want to run TRIM regularly, you can simply create a cronjob that runs the fstrim command for you. Here's how to make a barebones cron job that will do this automatically.

First, run the following command to open the nano text editor with root permissions:

sudo nano /etc/cron.daily/fstrim

Type the following code into the file:

#!/bin/sh

fstrim /

create-fstrim-cron-job-ubuntu

Save the file by pressing Ctrl+O and press Enter to confirm. Press Ctrl+X to close nano after saving the file.

Last, run the following command to make the script executable:

sudo chmod +x /etc/cron.daily/fstrim

sudo-chmod-x-make-executable

Ubuntu will now run fstrim on a schedule, just as it does other system maintenance tasks.


Note that TRIM is only supported on modern file systems, so you'll need something like ext4 and not ext3 or ext2. If you don't know what file system you're using, don't worry -- ext4 is selected by default.

Much of this advice also applies to other Linux distributions. While Linux did implement TRIM support in the kernel long ago, its TRIM support seems to have never been enabled by default for typical users in Linux distributions.

Image Credit: Mace Ojala on Flickr (cropped)